package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.adapter.CustomAbstractListAdapter;
import cn.com.dhc.mibd.eufw.widget.android.ptr.CustomPullToRefreshListView;

/**
 * Abstract ListView.
 *
 * @author guodong@dhc.com.cn
 */
@Deprecated
public abstract class CustomAbstractListView {

    /**
     * PullToRefreshListView
     */
    protected CustomPullToRefreshListView ptrListView = null;

    /**
     * listView
     */
    protected ListView listView = null;

    /**
     * Footer View
     */
    protected View footerView = null;

    /**
     * AsyncTaskActivityDelegate
     */
    protected Context context;

    /**
     * OnScrollListener
     */
    private OnScrollListener onScrollListener = null;

    /**
     * Constructor
     */
    public CustomAbstractListView(Context context, ListView listView) {
        this.context = context;
        this.listView = listView;
        onInitialize();
    }

    /**
     * Constructor
     */
    public CustomAbstractListView(Context context, CustomPullToRefreshListView ptrListView) {
        this.context = context;
        this.listView = ptrListView.getRefreshableView();
        this.ptrListView = ptrListView;
        onInitialize();
    }

    /**
     * Initialize ListView
     */
    protected void onInitialize() {
        listView.setSmoothScrollbarEnabled(true);
        listView.setFastScrollEnabled(false);
        // 短按
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CustomAbstractListView.this.onItemClickListener(parent, view, position, id);
            }
        });

        // 弹出菜单
        listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                CustomAbstractListView.this.onCreateContextMenu(menu, v, menuInfo);
            }
        });

        // 滚动事件
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (onScrollListener != null) {
                    onScrollListener.onScrollStateChanged(view, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount == totalItemCount) && (totalItemCount != 0)) {
                    CustomAbstractListView.this.onScrollToEnd(view, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                if (onScrollListener != null) {
                    onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                }
            }
        });
    }

    /**
     * Set OnItemClickListener
     */
    public abstract void onItemClickListener(AdapterView<?> parent, View view, int position, long id);

    /**
     * OnCreateContextMenu
     */
    public abstract void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);

    /**
     * OnScrollListener Scroll to end
     */
    public void onScrollToEnd(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }

    public Context getContext() {
        return context;
    }

    /**
     * Get visibility
     */
    public int getVisibility() {
        if (ptrListView != null) {
            return ptrListView.getVisibility();
        } else {
            return listView.getVisibility();
        }
    }

    /**
     * Set visibility
     */
    public void setVisibility(int visibility) {
        if (ptrListView != null) {
            ptrListView.setVisibility(visibility);
            listView.setVisibility(visibility);
        } else {
            listView.setVisibility(visibility);
        }
    }

    /**
     * Get adapter
     */
    public CustomAbstractListAdapter<?> getAdapter() {
        if (listView.getHeaderViewsCount() > 0 || listView.getFooterViewsCount() > 0) {
            if (listView.getAdapter() != null) {
                return (CustomAbstractListAdapter<?>) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter();
            }
        }
        return (CustomAbstractListAdapter<?>) listView.getAdapter();
    }

    /**
     * Set adapter
     */
    public void setAdapter(CustomAbstractListAdapter<?> listAdapter) {
        listView.setAdapter(listAdapter);
    }

    public void addHeaderView(View v) {
        listView.addHeaderView(v);
    }

    public void removeHeaderView(View v) {
        listView.removeHeaderView(v);
    }

    public void setSelectionFromTop(int position, int y) {
        listView.setSelectionFromTop(position, y);
    }

    /**
     * 设置查看更多。
     */
    protected void setFooterViewVisible(boolean visible) {
        if (visible) {
            if (footerView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                footerView = inflater.inflate(R.layout.footer_look_for_more, null);
                addFooterView(footerView);
            } else {
                footerView.findViewById(R.id.llyt_item_layout_ing).setVisibility(View.GONE);
                footerView.findViewById(R.id.llyt_item_layout_next).setVisibility(View.VISIBLE);
            }
        } else {
            if (footerView != null) {
                removeFooterView(footerView);
                footerView = null;
            }
        }
    }

    public void addFooterView(View v) {
        listView.addFooterView(v);
    }

    public void removeFooterView(View v) {
        listView.removeFooterView(v);
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    public void setTag(final Object tag) {
        listView.setTag(tag);
    }

    public final View findViewWithTag(Object tag) {
        return listView.findViewWithTag(tag);
    }

    public interface OnScrollListener {
        void onScrollStateChanged(AbsListView view, int scrollState);

        void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
    }
}
